#include <MysqlController.h>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QApplication>
#include <QDebug>
#include<QSqlRecord>
#include <QTextStream>
#include <QStringList>
#include <map>
using namespace std;

map<QString,int> exist;

MysqlController::MysqlController(){
    QSqlDatabase dbcon = QSqlDatabase::addDatabase("QODBC");  // ODBC 驱动
    dbcon.setHostName("127.0.0.1");                // MySQL 服务器ip地址，本机
    dbcon.setPort(3306);                          // 端口号，默认
    dbcon.setDatabaseName("persondb");           // ODBC 数据源名称
    bool ok = dbcon.open();
    if(!ok)
        qDebug() << "Error,  persondatabase 数据库文件打开失败！";
    else
        qDebug() << "Sucess, persondatabase 数据库文件打开成功！";
}



int timeToMinutes(const QString& timeStr) {
    QStringList parts = timeStr.split(':');
    int hours = parts[0].toInt();
    int minutes = parts[1].toInt();
    return hours * 60 + minutes;
}

QString CountDif(QString a,QString b){
    int t=timeToMinutes(b)-timeToMinutes(a);
    if(t<0) t+=1440;
    return QString::number(t/60)+"时"+QString::number(t%60)+"分";
}

void ToFlightinfo(flightinfo &t,QSqlQuery &query)
{
    for(int i=0;i<query.record().count();i++)
    {

        if(i==0) t.orgcty=query.record().value(i).toString();
        if(i==1) t.Pftn_Org=query.record().value(i).toString();
        if(i==2) t.Prvn_Org=query.record().value(i).toString();
        if(i==3) t.orgap=query.record().value(i).toString();
        if(i==4) t.dstcty=query.record().value(i).toString();
        if(i==5) t.Pftn_Dstnt=query.record().value(i).toString();
        if(i==6) t.Prvn_Dstnt=query.record().value(i).toString();
        if(i==7) t.desap=query.record().value(i).toString();
        if(i==8) t.airlines=query.record().value(i).toString();
        if(i==9) t.flight=query.record().value(i).toString();
        if(i==10) t.arcfttp=query.record().value(i).toString();
        if(i==11) t.dpttm=query.record().value(i).toString();
        if(i==12) t.arvtm=query.record().value(i).toString();
        if(i==13) t.ptalt=query.record().value(i).toString();
        if(i==14) t.Mon=query.record().value(i).toString();
        if(i==15) t.Tur=query.record().value(i).toString();
        if(i==16) t.Wes=query.record().value(i).toString();
        if(i==17) t.Thur=query.record().value(i).toString();
        if(i==18) t.Fri=query.record().value(i).toString();
        if(i==19) t.Sat=query.record().value(i).toString();
        if(i==20) t.Sun=query.record().value(i).toString();
        if(i==21) t.dirtime=query.record().value(i).toString();
        if(i==22) t.eco_price=query.record().value(i).toInt();
        if(i==23) t.fir_price=query.record().value(i).toInt();
        if(i==24) t.id=query.record().value(i).toInt();
        if(i==25) t.buyer_name=query.record().value(i).toString();
        if(i==26) t.passenger_name=query.record().value(i).toString();
        if(i==27) t.buyer_idnum=query.record().value(i).toString();
        if(i==28) t.passenger_idnum=query.record().value(i).toString();
        if(i==29) t.month=query.record().value(i).toInt();
        if(i==30) t.day=query.record().value(i).toInt();
        if(i==31) t.weekday=query.record().value(i).toString();
    };
}

int MysqlController::Login(QString username,QString password,int type) {
    QSqlQuery query;
    if(type==0) query.exec("SELECT * FROM users");
    else query.exec("SELECT * FROM admins");
    while(query.next())
    {
        QString name = query.value(0).toString();
        QString pwd = query.value(1).toString();
        if(name==username&&pwd==password)
        {
            qDebug() << " 登录成功"  ;
            return 0;
        }
    }
    qDebug() << "账号或密码错误"  ;
    return 1;
}

void MysqlController::Certify(QString username,QString name,int age,QString gender,QString idnum)
{
    QSqlQuery query;
    query.prepare("UPDATE users SET name = \""+name+"\", age = "+QString::number(age)+", gender = \""+gender+"\", idnumber=\""+idnum+"\" WHERE username = \""+username+"\"");
    if(query.exec()) qDebug()<<"实名成功";
    else qDebug()<<"实名失败";
}

int MysqlController::Register(QString username,QString password,int type) {
    QSqlQuery query;
    if(type==0) query.exec("SELECT * FROM users");
    else query.exec("SELECT * FROM admins");
    while(query.next())
    {
        QString name = query.value(0).toString();
        if(name==username)
        {
            qDebug() << "用户名已存在，请换一个用户名" << name  ;
            return 1;
        }
    }
    bool re;
    if(type==0)  re=query.exec("INSERT INTO users values(\"" + username + "\",\"" + password +"\",\"null\",-1,\"null\",\"null\")");
    else re=query.exec("INSERT INTO admins values(\"" + username + "\",\"" + password +"\")");
    if(!re) {qDebug() << "注册失败，未知错误 ";return 2;}
    else
    {
        qDebug() << "注册成功,账号 "<<username<<" 密码 "<<password ;
        return 0;
    }
}


QString MysqlController::FindName(QString username)
{
    QSqlQuery query;
    query.exec("select * from users where username = \""+username+"\"");

    QString name;
    while (query.next()) {
        for(int i=0;i<query.record().count();i++) if(i==2) name=query.record().value(i).toString();
    }
    qDebug()<<name;
    return name;
}

QString MysqlController::FindIdnum(QString username)
{
    QSqlQuery query;
    query.exec("select * from users where username = \""+username+"\"");

    QString idnum;
    while (query.next()) {
        for(int i=0;i<query.record().count();i++) if(i==5) idnum=query.record().value(i).toString();
    }
    qDebug()<<idnum;
    return idnum;
}

void MysqlController::ClearUser(int type) {
    QSqlQuery query;
    if(type==0) query.exec("DELETE FROM users");
    else query.exec("DELETE FROM admins");
}

void MysqlController::DeleteUser(QString name,int type){
    QSqlQuery query;
    if(type==0) query.exec("DELETE FROM users where username = \""+name+"\"");
    else query.exec("DELETE FROM admins where username = \""+name+"\"");
}

void MysqlController::FindFlights(QString orgcty,QString dstcty,QString weekday){
    flightinfos.clear();
    QSqlQuery query;
    query.prepare("select * from flights_info where orgcty = \""+orgcty+"\" and Dstntcty = \""+dstcty+"\" and Frqcy_"+weekday+" = 1;");
    if(query.exec())
    {
        qDebug()<<"查找成功";
        while (query.next()) {
            flightinfo rec;
            ToFlightinfo(rec,query);
            flightinfos.push_back(rec);
        }
    }
    else qDebug()<<"查找错误";
}

QString nextday(QString s){
    if(s=="Mon") return "Tur";
    if(s=="Tur") return "Wes";
    if(s=="Wes") return "Thur";
    if(s=="Thur") return "Fri";
    if(s=="Fri") return "Sat";
    if(s=="Sat") return "Sun";
    if(s=="Sun") return "Mon";
    return "Error";
}

void MysqlController::FindTrans(QString orgcty,QString dstcty,QString weekday){
    trans1.clear();
    trans2.clear();
    exist.clear();
    QVector<flightinfo> t;
    QSqlQuery query;
    query.prepare("select * from flights_info where orgcty = \""+orgcty+"\" and Frqcy_"+weekday+" = 1;");
    if(query.exec())
    {
        while (query.next()) {
            flightinfo rec;
            ToFlightinfo(rec,query);
            if(exist[rec.dstcty]==0) {t.push_back(rec);exist[rec.dstcty]++;}
            if(t.size()>=30) break;
        }
        qDebug()<<t.size();

    }

    for(int i=0;i<t.size();i++)
    {
        if(t[i].arvtm.size()==4&&(t[i].arvtm[0]=='1'||t[i].arvtm[0]=='0'))
        {
            query.prepare("select * from flights_info where orgcty = \""+t[i].dstcty+"\" and Dstntcty = \""+dstcty+"\" and Frqcy_"+nextday(weekday)+" = 1 and TIMESTAMPDIFF(MINUTE, STR_TO_DATE(\""+t[i].arvtm+"\", '%H:%i') , STR_TO_DATE(Dpttm, '%H:%i')) > 30 and TIMESTAMPDIFF(MINUTE, STR_TO_DATE(\""+t[i].arvtm+"\", '%H:%i') , STR_TO_DATE(Dpttm, '%H:%i')) < 420;;");
            if(query.exec())
            {
                while (query.next()) {
                    flightinfo rec;
                    ToFlightinfo(rec,query);
                    if(trans2.size()==0||rec.orgcty!=trans2[trans2.size()-1].orgcty)
                    {
                        trans1.push_back(t[i]);
                        trans2.push_back(rec);
                    }
                }
            }
        }
        else
        {
            query.prepare("select * from flights_info where orgcty = \""+t[i].dstcty+"\" and Dstntcty = \""+dstcty+"\" and Frqcy_"+weekday+" = 1 and TIMESTAMPDIFF(MINUTE, STR_TO_DATE(\""+t[i].arvtm+"\", '%H:%i') , STR_TO_DATE(Dpttm, '%H:%i')) > 30  and TIMESTAMPDIFF(MINUTE, STR_TO_DATE(\""+t[i].arvtm+"\", '%H:%i') , STR_TO_DATE(Dpttm, '%H:%i')) < 420;;");
            if(query.exec())
            {
                while (query.next()) {
                    flightinfo rec;
                    ToFlightinfo(rec,query);
                    if(trans2.size()==0||rec.orgcty!=trans2[trans2.size()-1].orgcty)
                    {
                        trans1.push_back(t[i]);
                        trans2.push_back(rec);
                    }
                }
            }

            query.prepare("select * from flights_info where orgcty = \""+t[i].dstcty+"\" and Dstntcty = \""+dstcty+"\" and Frqcy_"+nextday(weekday)+" = 1 and TIMESTAMPDIFF(MINUTE, STR_TO_DATE(\""+t[i].arvtm+"\", '%H:%i') , STR_TO_DATE(Dpttm, '%H:%i')) +1440 > 30  and TIMESTAMPDIFF(MINUTE, STR_TO_DATE(\""+t[i].arvtm+"\", '%H:%i') , STR_TO_DATE(Dpttm, '%H:%i')) + 1440 < 420;;");
            if(query.exec())
            {
                while (query.next()) {
                    flightinfo rec;
                     ToFlightinfo(rec,query);
                    if(trans2.size()==0||rec.orgcty!=trans2[trans2.size()-1].orgcty)
                    {
                        trans1.push_back(t[i]);
                        trans2.push_back(rec);
                    }
                }
            }

        }
    }
}



void MysqlController::FindRoundWays(QString orgcty,QString dstcty,QString day1,QString day2)
{
    QSqlQuery query;
    QVector<flightinfo> t1,t2;
    query.exec("select * from flights_info where orgcty = \""+orgcty+"\" and Dstntcty = \""+dstcty+"\" and Frqcy_"+day1+" = 1;");
    while (query.next()) {
        flightinfo rec;
        ToFlightinfo(rec,query);
        round1.push_back(rec);
    }



    query.exec("select * from flights_info where orgcty = \""+dstcty+"\" and Dstntcty = \""+orgcty+"\" and Frqcy_"+day2+" = 1;");
    while (query.next()) {
        flightinfo rec;
        ToFlightinfo(rec,query);
        round2.push_back(rec);
    }

}

void MysqlController::FindMultiWays(QString cty1,QString cty2,QString cty3,QString day1,QString day2)
{
    QSqlQuery query;
    QVector<flightinfo> t1,t2;
    query.exec("select * from flights_info where orgcty = \""+cty1+"\" and Dstntcty = \""+cty2+"\" and Frqcy_"+day1+" = 1;");
    while (query.next()) {
        flightinfo rec;
        ToFlightinfo(rec,query);
        multi1.push_back(rec);
    }

    query.exec("select * from flights_info where orgcty = \""+cty2+"\" and Dstntcty = \""+cty3+"\" and Frqcy_"+day2+" = 1;");
    while (query.next()) {
        flightinfo rec;
        ToFlightinfo(rec,query);
        multi2.push_back(rec);
    }
}

void MysqlController::DeleteOrder_dir(int id){
    QSqlQuery query;
    query.prepare("delete from orders_dir where id = "+ QString::number(id) +";");
    qDebug()<<id;
    if(query.exec()) qDebug()<<"删除成功";
}



void MysqlController::AddOrder_dir(flightinfo f,QString buyer_name,QString buyer_idnum,QString passenger_name,QString passenger_idnum,int month,int day,QString weekday){

    QSqlQuery query;
    query.prepare("SELECT * FROM orders_dir");
    int cnt=0;
    while(query.next()) cnt++;

    query.prepare("INSERT INTO orders_dir values(\"" + f.orgcty + "\",\"" + f.Pftn_Org +"\",\"" + f.Prvn_Org +"\",\"" + f.orgap +"\",\"" + f.dstcty +"\",\"" + f.Pftn_Dstnt +"\",\"" + f.Prvn_Dstnt +"\",\"" + f.desap+"\",\"" + f.airlines+"\",\"" + f.flight+"\",\"" + f.arcfttp+"\",\"" + f.dpttm+"\",\"" + f.arvtm+"\",\"" + f.ptalt+"\",\"" + f.Mon+"\",\"" + f.Tur+"\",\"" + f.Wes+"\",\"" + f.Thur+"\",\"" + f.Fri+"\",\"" + f.Sat+"\",\"" + f.Sun+"\",\"" + f.dirtime+"\"," + QString::number(f.eco_price)+"," + QString::number(f.fir_price)+"," + QString::number(cnt) +",\"" +buyer_name +"\",\""+passenger_name + "\",\""+buyer_idnum+"\",\""+passenger_idnum+"\"," + QString::number(month)+","+QString::number(day)+",\""+weekday+"\");");

    if(query.exec()) qDebug()<<"插入成功";
    else qDebug()<<"插入失败";
}

void MysqlController::AddOrder_trans(flightinfo trans1,flightinfo trans2,QString buyer_name,QString buyer_idnum,QString passenger_name,QString passenger_idnum,int month,int day,QString weekday){

    QSqlQuery query;
    query.prepare("SELECT * FROM orders_dir");
    int cnt=0;
    while(query.next()) cnt++;

    flightinfo f=trans1;
    query.prepare("INSERT INTO orders_dir values(\"" + f.orgcty + "\",\"" + f.Pftn_Org +"\",\"" + f.Prvn_Org +"\",\"" + f.orgap +"\",\"" + f.dstcty +"\",\"" + f.Pftn_Dstnt +"\",\"" + f.Prvn_Dstnt +"\",\"" + f.desap+"\",\"" + f.airlines+"\",\"" + f.flight+"\",\"" + f.arcfttp+"\",\"" + f.dpttm+"\",\"" + f.arvtm+"\",\"" + f.ptalt+"\",\"" + f.Mon+"\",\"" + f.Tur+"\",\"" + f.Wes+"\",\"" + f.Thur+"\",\"" + f.Fri+"\",\"" + f.Sat+"\",\"" + f.Sun+"\",\"" + f.dirtime+"\"," + QString::number(f.eco_price)+"," + QString::number(f.fir_price)+"," + QString::number(cnt) +",\"" +buyer_name +"\",\""+passenger_name + "\",\""+buyer_idnum+"\",\""+passenger_idnum+"\"," + QString::number(month)+","+QString::number(day)+",\""+weekday+"\");");
    if(query.exec()) qDebug()<<"插入成功";
    f=trans2;
    query.prepare("INSERT INTO orders_dir values(\"" + f.orgcty + "\",\"" + f.Pftn_Org +"\",\"" + f.Prvn_Org +"\",\"" + f.orgap +"\",\"" + f.dstcty +"\",\"" + f.Pftn_Dstnt +"\",\"" + f.Prvn_Dstnt +"\",\"" + f.desap+"\",\"" + f.airlines+"\",\"" + f.flight+"\",\"" + f.arcfttp+"\",\"" + f.dpttm+"\",\"" + f.arvtm+"\",\"" + f.ptalt+"\",\"" + f.Mon+"\",\"" + f.Tur+"\",\"" + f.Wes+"\",\"" + f.Thur+"\",\"" + f.Fri+"\",\"" + f.Sat+"\",\"" + f.Sun+"\",\"" + f.dirtime+"\"," + QString::number(f.eco_price)+"," + QString::number(f.fir_price)+"," + QString::number(cnt) +",\"" +buyer_name +"\",\""+passenger_name + "\",\""+buyer_idnum+"\",\""+passenger_idnum+"\"," + QString::number(month)+","+QString::number(day)+",\""+weekday+"\");");

    if(query.exec()) qDebug()<<"插入成功";
    else qDebug()<<"插入失败";
}

void MysqlController::AddOrder_rounds(flightinfo round1,flightinfo round2,QString buyer_name,QString buyer_idnum,QString passenger_name,QString passenger_idnum,int month,int day,QString weekday){

    QSqlQuery query;
    query.prepare("SELECT * FROM orders_dir");
    int cnt=0;
    while(query.next()) cnt++;

    flightinfo f=round1;
    query.prepare("INSERT INTO orders_dir values(\"" + f.orgcty + "\",\"" + f.Pftn_Org +"\",\"" + f.Prvn_Org +"\",\"" + f.orgap +"\",\"" + f.dstcty +"\",\"" + f.Pftn_Dstnt +"\",\"" + f.Prvn_Dstnt +"\",\"" + f.desap+"\",\"" + f.airlines+"\",\"" + f.flight+"\",\"" + f.arcfttp+"\",\"" + f.dpttm+"\",\"" + f.arvtm+"\",\"" + f.ptalt+"\",\"" + f.Mon+"\",\"" + f.Tur+"\",\"" + f.Wes+"\",\"" + f.Thur+"\",\"" + f.Fri+"\",\"" + f.Sat+"\",\"" + f.Sun+"\",\"" + f.dirtime+"\"," + QString::number(f.eco_price)+"," + QString::number(f.fir_price)+"," + QString::number(cnt) +",\"" +buyer_name +"\",\""+passenger_name + "\",\""+buyer_idnum+"\",\""+passenger_idnum+"\"," + QString::number(month)+","+QString::number(day)+",\""+weekday+"\");");
    if(query.exec()) qDebug()<<"插入成功";
    f=round2;
    query.prepare("INSERT INTO orders_dir values(\"" + f.orgcty + "\",\"" + f.Pftn_Org +"\",\"" + f.Prvn_Org +"\",\"" + f.orgap +"\",\"" + f.dstcty +"\",\"" + f.Pftn_Dstnt +"\",\"" + f.Prvn_Dstnt +"\",\"" + f.desap+"\",\"" + f.airlines+"\",\"" + f.flight+"\",\"" + f.arcfttp+"\",\"" + f.dpttm+"\",\"" + f.arvtm+"\",\"" + f.ptalt+"\",\"" + f.Mon+"\",\"" + f.Tur+"\",\"" + f.Wes+"\",\"" + f.Thur+"\",\"" + f.Fri+"\",\"" + f.Sat+"\",\"" + f.Sun+"\",\"" + f.dirtime+"\"," + QString::number(f.eco_price)+"," + QString::number(f.fir_price)+"," + QString::number(cnt) +",\"" +buyer_name +"\",\""+passenger_name + "\",\""+buyer_idnum+"\",\""+passenger_idnum+"\"," + QString::number(month)+","+QString::number(day)+",\""+weekday+"\");");

    if(query.exec()) qDebug()<<"插入成功";
    else qDebug()<<"插入失败";
}

void MysqlController::AddOrder_multis(flightinfo round1,flightinfo round2,QString buyer_name,QString buyer_idnum,QString passenger_name,QString passenger_idnum,int month,int day,QString weekday){

    QSqlQuery query;
    query.prepare("SELECT * FROM orders_dir");
    int cnt=0;
    while(query.next()) cnt++;

    flightinfo f=round1;
    query.prepare("INSERT INTO orders_dir values(\"" + f.orgcty + "\",\"" + f.Pftn_Org +"\",\"" + f.Prvn_Org +"\",\"" + f.orgap +"\",\"" + f.dstcty +"\",\"" + f.Pftn_Dstnt +"\",\"" + f.Prvn_Dstnt +"\",\"" + f.desap+"\",\"" + f.airlines+"\",\"" + f.flight+"\",\"" + f.arcfttp+"\",\"" + f.dpttm+"\",\"" + f.arvtm+"\",\"" + f.ptalt+"\",\"" + f.Mon+"\",\"" + f.Tur+"\",\"" + f.Wes+"\",\"" + f.Thur+"\",\"" + f.Fri+"\",\"" + f.Sat+"\",\"" + f.Sun+"\",\"" + f.dirtime+"\"," + QString::number(f.eco_price)+"," + QString::number(f.fir_price)+"," + QString::number(cnt) +",\"" +buyer_name +"\",\""+passenger_name + "\",\""+buyer_idnum+"\",\""+passenger_idnum+"\"," + QString::number(month)+","+QString::number(day)+",\""+weekday+"\");");
    if(query.exec()) qDebug()<<"插入成功";
    f=round2;
    query.prepare("INSERT INTO orders_dir values(\"" + f.orgcty + "\",\"" + f.Pftn_Org +"\",\"" + f.Prvn_Org +"\",\"" + f.orgap +"\",\"" + f.dstcty +"\",\"" + f.Pftn_Dstnt +"\",\"" + f.Prvn_Dstnt +"\",\"" + f.desap+"\",\"" + f.airlines+"\",\"" + f.flight+"\",\"" + f.arcfttp+"\",\"" + f.dpttm+"\",\"" + f.arvtm+"\",\"" + f.ptalt+"\",\"" + f.Mon+"\",\"" + f.Tur+"\",\"" + f.Wes+"\",\"" + f.Thur+"\",\"" + f.Fri+"\",\"" + f.Sat+"\",\"" + f.Sun+"\",\"" + f.dirtime+"\"," + QString::number(f.eco_price)+"," + QString::number(f.fir_price)+"," + QString::number(cnt) +",\"" +buyer_name +"\",\""+passenger_name + "\",\""+buyer_idnum+"\",\""+passenger_idnum+"\"," + QString::number(month)+","+QString::number(day)+",\""+weekday+"\");");

    if(query.exec()) qDebug()<<"插入成功";
    else qDebug()<<"插入失败";
}

void MysqlController::ClearOrders(){
    QSqlQuery query;
    query.exec("TRUNCATE TABLE orders_dir");
    query.exec("TRUNCATE TABLE orders_trans");
    query.exec("TRUNCATE TABLE orders_round");
    query.exec("TRUNCATE TABLE orders_multi");
}

void MysqlController::ShowOrders(QString name){
    order_dir.clear();
    order_trans1.clear();
    order_trans2.clear();

    QSqlQuery query;
    query.prepare("select * from orders_dir where passenger_name = \""+name+"\"");
    if(query.exec())
    {
        qDebug()<<"查找成功";
        while (query.next()) {
            flightinfo rec;
            ToFlightinfo(rec,query);
            order_dir.push_back(rec);
        }
    }
    else qDebug()<<"查找错误";
    qDebug()<<order_dir.size()<<' '<<order_trans1.size()<<' '<<order_round1.size()<<' '<<order_multi1.size();
}


void MysqlController::SearchOrders(QString idnum){
    order_dir.clear();
    order_trans1.clear();
    order_trans2.clear();

    QSqlQuery query;
    query.prepare("select * from orders_dir where passenger_idnum = \""+idnum+"\"");
    if(query.exec())
    {
        qDebug()<<"查找成功";
        while (query.next()) {
            flightinfo rec;
            ToFlightinfo(rec,query);
            order_dir.push_back(rec);
        }
    }
}


void MysqlController::SearchFlight(QString info){
    QSqlQuery query;
    query.exec("select * from flights_info where Flight = \""+info+"\"");

    while (query.next()) {
        flightinfo rec;
        ToFlightinfo(rec,query);
        SearchInfo.push_back(rec);
    }
    qDebug()<<SearchInfo.size();
}



int MysqlController::AddRelatives(QString name,QString relative_name,QString relative_idnum)
{
    QSqlQuery query;
    query.exec("select * from relatives");
    while(query.next())
    {
        QString name2=query.value(0).toString();
        QString idnum = query.value(2).toString();
        if(idnum==relative_idnum&&name2==name)
        {
            qDebug() << "亲属已存在" << name  ;
            return 1;
        }
    }


    query.prepare("INSERT INTO relatives (main_name, name, idnum) VALUES (\""+name+"\", \""+relative_name+"\", \""+relative_idnum+"\")");
    if(query.exec()) qDebug()<<"添加成功";
    else qDebug()<<"添加失败";
    return 0;
}

void MysqlController::SearchRelatives(QString name)
{
    QSqlQuery query;
    query.prepare("SELECT * FROM relatives WHERE main_name = \""+name+"\"");

    if(query.exec())
    {
        while (query.next()) {
            person p;
            for(int i=0;i<query.record().count();i++)
            {
               if(i==1) p.name=query.record().value(i).toString();
               if(i==2) p.idnum=query.record().value(i).toString();
            }
            relatives.push_back(p);
        }

        for(int i=0;i<relatives.size();i++) qDebug()<<relatives[i].name<<' '<<relatives[i].idnum;

    }
}



